<?php
 class Pictures extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
     /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getList(){
        $this->db->order_by('pictures.CreatedDate DESC');
        $query = $this->db->get('pictures');
        $res = $query->result_array();
        return $res;
    }
    
    
    /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getCount(){
        $this->db->select();
        $this->db->from('pictures');
        $query = $this->db->get();
        $res = $query->num_rows();
        return $res;
    }
    
        
    /**
     * Get product by product id
     * @author tien.nguyen
     */
    public function getPicture($pictureId){
        $query = $this->db->get_where('pictures', array('PictureId' => $pictureId));
        return $query->row_array();
    }
    
    /**
     * edit function in config
     * @author tien.nguyen
     */
    public function edit($data, $pictureId){
        $data = $this->_convertData($data);
        return $this->db->update('pictures', $data, array('PictureId' => $pictureId));
    }
    
    /**
     * Delete product id
     * @author tien.nguyen
     */
    public function delete($pictureId){
       return $this->db->delete('pictures', array('PictureId' => $pictureId)); 
    }
    
    /**
     * add product
     * @author tien.nguyen
     */
    public function add($data){
        $data = $this->_convertData($data);
        $data['CreatedDate'] = date('Y-m-d H:m:s');
        return $this->db->insert('pictures', $data);
    }
    
    /**
     * Convert data array to array of product
     * @author tien.nguyen
     */
    private function _convertData($data){
        $res = array();
        $arr = array('PictureId', 'PictureName', 'PictureTitle', 
            'PictureLink', 'CreatedDate', 'Visible');
        foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
        }
        
        //set default value
        $res['Visible'] = isset($res['Visible']) ? $res['Visible'] : 0;
        return $res;
    }
 }
?>